package com.ly.erms.modular.baseconfig.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ly.erms.common.annotion.BussinessLog;
import com.ly.erms.common.annotion.Permission;
import com.ly.erms.common.constant.dicmap.ClassificationDict;
import com.ly.erms.common.constant.factory.ConstantFactory;
import com.ly.erms.common.exception.BizExceptionEnum;
import com.ly.erms.common.persistence.dao.BaseconfigClassificationMapper;
import com.ly.erms.common.persistence.model.BaseconfigClassification;
import com.ly.erms.core.base.controller.BaseController;
import com.ly.erms.core.exception.ErmsException;
import com.ly.erms.core.log.LogObjectHolder;
import com.ly.erms.core.node.ZTreeNode;
import com.ly.erms.core.shiro.ShiroKit;
import com.ly.erms.core.util.ToolUtil;
import com.ly.erms.modular.baseconfig.dao.ClassificationDao;
import com.ly.erms.modular.baseconfig.service.IBaseconfigClassificationService;
import com.ly.erms.modular.baseconfig.warpper.ClassificationWarpper;

/**
 * 分类方案控制器
 * 
 * @author liuyang
 * @date 2018年1月16日
 */
@Controller
@RequestMapping("/classification")
public class ClassificationController extends BaseController {
	private String PREFIX = "/baseconfig/classification/";

	@Resource
	ClassificationDao classificationDao;

	@Resource
	BaseconfigClassificationMapper classificationMapper;
	
	@Resource
	IBaseconfigClassificationService classificationService;

	@RequestMapping("")
	public String index() {
		return PREFIX + "classification.html";
	}

	/**
	 * 获取所有类目列表
	 */
	@RequestMapping("/list")
	@Permission
	@ResponseBody
	public Object list(@RequestParam(required = false) String classname,
			@RequestParam(required = false) String createuser, @RequestParam(required = false) String begintime,
			@RequestParam(required = false) String endtime) {
		List<Map<String, Object>> list = this.classificationDao.list(classname, createuser, begintime, endtime);
		return warpObject(new ClassificationWarpper(list));
	}

	/**
	 * 跳转到添加页面
	 */
	@RequestMapping("/classification_add")
	public String classificationAdd() {
		return PREFIX + "classification_add.html";
	}
	
	/**
	 * 跳转到修改页面
	 */
	@RequestMapping("/classification_update/{id}")
	public String classificationUpdate(@PathVariable Integer id,Model model) {
		BaseconfigClassification classification = classificationMapper.selectById(id);
		model.addAttribute("classification",classification);
		model.addAttribute("pName", ConstantFactory.me().getClassificationName(classification.getPid()));
		model.addAttribute("createuser", ConstantFactory.me().getUserNameById(classification.getCreateuser()));
		model.addAttribute("lastuser", ConstantFactory.me().getUserNameById(classification.getLastuser()));
		LogObjectHolder.me().set(classification);
		return PREFIX + "classification_edit.html";
	}

	/**
	 * 获取类目的tree列表
	 */
	@RequestMapping("/tree")
	@ResponseBody
	public List<ZTreeNode> tree() {
		List<ZTreeNode> tree = this.classificationDao.tree();
		tree.add(ZTreeNode.createParent());
		return tree;
	}

	/**
	 * 添加类目
	 */
	@BussinessLog(value = "新增类目", key = "classname", dict = ClassificationDict.class)
	@RequestMapping("/add")
	@ResponseBody
	public Object AddClassification(BaseconfigClassification classification) {
		if (ToolUtil.isOneEmpty(classification, classification.getClassname())) {
			throw new ErmsException(BizExceptionEnum.REQUEST_NULL);
		}
		EntityWrapper<BaseconfigClassification> wrapper = new EntityWrapper<>();
		Wrapper<BaseconfigClassification> eq = wrapper.eq("classname", classification.getClassname());
		Integer count = classificationMapper.selectCount(eq);
		if (count != 0) {
			throw new ErmsException(BizExceptionEnum.EXISTED_CLASSIFICATION);
		}
		
		classification.setCreateuser(ShiroKit.getUser().getId());
		classification.setCreatedate(new Date());
		classification.setLastuser(ShiroKit.getUser().getId());
		classification.setLastdate(new Date());
		ClassificationSetPids(classification);

		classificationMapper.insert(classification);
		return SUCCESS_TIP;
	}
	
	/**
	 * 修改类目
	 */
	@BussinessLog(value = "修改类目" ,key = "classname" , dict = ClassificationDict.class)
	@RequestMapping("/update")
	@ResponseBody
	public Object ModClassification(BaseconfigClassification classification){
		if (ToolUtil.isOneEmpty(classification, classification.getClassname())) {
			throw new ErmsException(BizExceptionEnum.REQUEST_NULL);
		}
		
		EntityWrapper<BaseconfigClassification> wrapper = new EntityWrapper<>();
		Wrapper<BaseconfigClassification> eq = wrapper.eq("classname", classification.getClassname());
		Integer count = classificationMapper.selectCount(eq);
		if (count != 0) {
			throw new ErmsException(BizExceptionEnum.EXISTED_CLASSIFICATION);
		}
		classification.setLastuser(ShiroKit.getUser().getId());
		classification.setLastdate(new Date());
		classificationMapper.updateById(classification);
		return SUCCESS_TIP;
	}
	
	/**
	 * 删除类目
	 * @param classification
	 */
	@BussinessLog(value = "删除类目" ,key = "classname" , dict = ClassificationDict.class)
	@RequestMapping("/delete")
	@ResponseBody
	public Object DelClassification(@RequestParam Integer classificationId){
		classificationService.deleteClassification(classificationId);
		return SUCCESS_TIP;
	}

	/**
	 * 设置pids
	 * @param classification
	 */
	private void ClassificationSetPids(BaseconfigClassification classification) {
		if (ToolUtil.isEmpty(classification.getPid()) || classification.getPid() == 0) {
			classification.setPid(0);
			classification.setPids("[0],");
		} else {
			Integer pid = classification.getPid();
			BaseconfigClassification temp = classificationMapper.selectById(pid);
			classification.setPid(temp.getId());
			classification.setPids(temp.getPids() + "[" + temp.getId() + "],");
		}
	}
	
}
